package com.fmy.jurisdiction.modular.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.fmy.jurisdiction.core.commons.consts.SystemConsts;
import com.fmy.jurisdiction.core.commons.kits.BizExceptionKit;
import com.fmy.jurisdiction.core.commons.kits.MybatisPlusKit;
import com.fmy.jurisdiction.modular.model.AdminDept;
import com.fmy.jurisdiction.modular.mapper.AdminDeptMapper;
import com.fmy.jurisdiction.modular.model.AdminUser;
import com.fmy.jurisdiction.modular.model.vo.AdminDeptChildren;
import com.fmy.jurisdiction.modular.service.IAdminDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fmy.jurisdiction.modular.service.IAdminUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author Mr.fmy
 * @since 2019-09-17
 */
@Service
public class AdminDeptServiceImpl extends ServiceImpl<AdminDeptMapper, AdminDept> implements IAdminDeptService {

    @Autowired
    private AdminDeptMapper deptMapper;
    @Autowired
    private IAdminUserService userService;

    @Override
    public boolean addAdminDept() {
        AdminDept dept = new AdminDept();
        dept.setId(SystemConsts.DEPT_ADMIN_ID)
                .setDeptName("总公司").setFid(SystemConsts.DEMP_FID)
                .setFids(SystemConsts.DEMP_FID.toString())
                .setDescription("默认总公司部门")
                .setSort(1);
        return deptMapper.addAdmin(dept) > 0;
    }

    @Override
    public List<Integer> fingByDeptSonId(Integer deptId) {
        return this.listObjs(MybatisPlusKit.<AdminDept>q().like(AdminDept.FIDS, deptId), item -> NumberUtil.parseInt(item.toString()));
    }

    @Override
    public boolean findDeptVail(Integer deptId, Integer deptSonId) {
        return this.count(MybatisPlusKit.<AdminDept>q().eq(AdminDept.ID, deptSonId).like(AdminDept.FIDS, deptId)) > 0;
    }

    @Override
    public List<AdminDeptChildren> getChildren(Integer deptId) {
        List<AdminDept> depts = this.list(MybatisPlusKit.<AdminDept>q()
                .like(deptId != null, AdminDept.FIDS, deptId)
                .or().eq(deptId != null, AdminDept.ID, deptId)
                .orderByAsc(AdminDept.SORT));
        List<AdminDeptChildren> childrens = new ArrayList<>();
        Integer topId = depts.size() > 1 ? childrens.stream().mapToInt(item -> item.getFid()).min().getAsInt() : depts.get(0).getFid();
        depts.forEach(item -> {
            if (item.getFid().equals(topId)) {
                AdminDeptChildren children = new AdminDeptChildren();
                BeanUtils.copyProperties(item, children);
                childrens.add(children);
            }
        });
        childrens.forEach(item -> {
            item.setChildren(recursion(item, depts));
        });
        return childrens;
    }

    @Override
    public boolean add(AdminDept dept) {
        AdminDept fDept = this.getById(dept.getFid());//获取父部门
        BizExceptionKit.throwBusiness(fDept == null, "父部门不存在");
        dept.setFids(fDept.getFids() + SystemConsts.ROLE_PERMI_SEPARATOR + fDept.getId());
        return this.save(dept);
    }

    @Override
    public boolean modify(Integer id, String name, Integer sort) {
        return this.update(MybatisPlusKit.<AdminDept>u().eq(AdminDept.ID, id)
                .set(AdminDept.DEPT_NAME, name)
                .set(AdminDept.SORT, sort));
    }

    @Override
    public boolean del(Integer id) {
        BizExceptionKit.throwBusiness(SystemConsts.DEPT_ADMIN_ID.equals(id), "该部门不可删除");
        BizExceptionKit.throwBusiness(userService.count(MybatisPlusKit.<AdminUser>q().eq(AdminUser.DEPT_ID, id)) > 0, "该部门存在关联用户、不可删除");
        return this.removeById(id);
    }

    private List<AdminDeptChildren> recursion(AdminDeptChildren children, List<AdminDept> depts) {
        List<AdminDeptChildren> childrens = new ArrayList<>();
        depts.forEach(item -> {
            if (item.equals(children.setFid(children.getId()))) {
                AdminDeptChildren childrenIten = new AdminDeptChildren();
                BeanUtils.copyProperties(item, childrenIten);
                childrens.add(childrenIten);
            }
        });
        childrens.forEach(item -> {
            item.setChildren(recursion(item, depts));
        });
        return childrens.size() > 0 ? childrens : null;
    }
}
